package typingballoon;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends GameCanvas implements Runnable {

    //midlet
    private TypingBalloonMIDlet midlet;
    private Display display;
    //path
    private String[] pathBalloon01 = new String[26];
    private String pathTitle = "";
    private String pathPlayOff = "";
    private String pathPlayOn = "";
    private String pathHighScoresOff = "";
    private String pathHighScoresOn = "";
    private String pathHelpOff = "";
    private String pathHelpOn = "";
    private String pathAboutOff = "";
    private String pathAboutOn = "";
    private String pathExit = "";
    //path level
    //images        
    private Image imgTitle;
    private Image imgPlayOff;
    private Image imgPlayOn;
    private Image imgHighScoresOff;
    private Image imgHighScoresOn;
    private Image imgHelpOff;
    private Image imgHelpOn;
    private Image imgAboutOff;
    private Image imgAboutOn;
    private Image imgExit;
    private int btnImgMenuWidth;
    private int btnImgMenuHeight;
    //private int btnImgLevelWidth;
    //private int btnImgLevelHeight;
    //button
    ButtonImage btnPlay;
    ButtonImage btnHighScores;
    ButtonImage btnHelp;
    ButtonImage btnAbout;
    ButtonImage btnExit;
    //font
    //canvas property
    private int CANVAS_WIDTH = getWidth();
    private int CANVAS_HEIGHT = getHeight();
    private int bgWidth = 0;
    private int warna;
    //positions
    private int x = 0;
    private int y = 0;
    private int xBg = 0;
    private int yBg = 0;
    private int[] xBalloon = new int[26];
    private int[] yBalloon = new int[26];
    private int jarakLevel = 0;
    private int xyArea[] = new int[4];
    private int xPlay, yPlay, xHighScores, yHighScores;
    private int xHelp, yHelp, xAbout, yAbout, xExit, yExit;
    //animated sprite
    private AnimatedSprite[] spBalloon01 = new AnimatedSprite[26];
    private static int FRAME_BALLOON_WIDTH = 0;
    private static int FRAME_BALLOON_HEIGHT = 0;
    private boolean running = false;
    //private BackgroundScroll bgScroll;
    private LayerManager lmgr;
    //speed 
    private int speedGame = 100;
    private int speedBalloon = 2;
    //private int speedGeser = 50;
    //private int speedPindahHuruf = 50;
    //huruf
    private String huruf = "";
    //random
    //private int iRand = 0;
    private Random myRandom = new Random();
    //boolean value    
    private boolean isStart = false;
    //disable and paused
    //boolean
    private boolean isDisablePortrait = false;
    private boolean isPaused = false;
    //button
    private ButtonImage btnResume;
    //image
    private Image imgBtnResume;
    private Image imgDisablePortrait;
    private int acuanDisable = 320;
    private String pathBtnResume = "";
    private String pathDisable = "/disableportrait.png";

    public MenuCanvas(TypingBalloonMIDlet midlet, Display display) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;
        this.display = display;

        CANVAS_WIDTH = getWidth();
        CANVAS_HEIGHT = getHeight();

        jarakLevel = (int) (CANVAS_WIDTH * 0.9);
        warna = 0xffffff;

        if (CANVAS_WIDTH == 640) {
            FRAME_BALLOON_WIDTH = 94;
            FRAME_BALLOON_HEIGHT = 100;

            for (int i = 0; i < 26; i++) {
                pathBalloon01[i] = "/balloon01_" + i + "_640.png";
            }

            pathPlayOff = "/btnPlayOff640.png";
            pathPlayOn = "/btnPlayOn640.png";
            pathHighScoresOff = "/btnHighScoresOff640.png";
            pathHighScoresOn = "/btnHighScoresOn640.png";
            pathHelpOff = "/btnHelpOff640.png";
            pathHelpOn = "/btnHelpOn640.png";
            pathAboutOff = "/btnAboutOff640.png";
            pathAboutOn = "/btnAboutOn640.png";
            pathExit = "/close640.png";
            pathTitle = "/title640.png";
            btnImgMenuWidth = 200;
            btnImgMenuHeight = 50;
            bgWidth = 1280;
        } else if (getWidth() > acuanDisable) {
            FRAME_BALLOON_WIDTH = 94;
            FRAME_BALLOON_HEIGHT = 100;

            pathTitle = "/title640.png";
            pathBtnResume = "/btnResume640.png";
            isDisablePortrait = true;
            isPaused = true;
        }
        {
            FRAME_BALLOON_WIDTH = 47;
            FRAME_BALLOON_HEIGHT = 50;

            for (int i = 0; i < 26; i++) {
                pathBalloon01[i] = "/balloon01_" + i + "_320.png";
            }

            pathPlayOff = "/btnPlayOff320.png";
            pathPlayOn = "/btnPlayOn320.png";
            pathHighScoresOff = "/btnHighScoresOff320.png";
            pathHighScoresOn = "/btnHighScoresOn320.png";
            pathHelpOff = "/btnHelpOff320.png";
            pathHelpOn = "/btnHelpOn320.png";
            pathAboutOff = "/btnAboutOff320.png";
            pathAboutOn = "/btnAboutOn320.png";
            pathExit = "/close320.png";
            pathTitle = "/title320.png";
            btnImgMenuWidth = 100;
            btnImgMenuHeight = 25;
            bgWidth = 640;
        }

        try {
            //animated sprite
            lmgr = new LayerManager();

            for (int i = 0; i < 26; i++) {
                spBalloon01[i] = new AnimatedSprite(Image.createImage(pathBalloon01[i]),
                        FRAME_BALLOON_WIDTH, FRAME_BALLOON_HEIGHT);
                spBalloon01[i].defineReferencePixel(FRAME_BALLOON_WIDTH / 2, FRAME_BALLOON_HEIGHT / 2);
                lmgr.append(spBalloon01[i]);
            }
        } catch (IOException ex) {
        }

        try {
            imgTitle = Image.createImage(pathTitle);
            imgPlayOff = Image.createImage(pathPlayOff);
            imgPlayOn = Image.createImage(pathPlayOn);
            imgHighScoresOff = Image.createImage(pathHighScoresOff);
            imgHighScoresOn = Image.createImage(pathHighScoresOn);
            imgHelpOff = Image.createImage(pathHelpOff);
            imgHelpOn = Image.createImage(pathHelpOn);
            imgAboutOff = Image.createImage(pathAboutOff);
            imgAboutOn = Image.createImage(pathAboutOn);
            imgExit = Image.createImage(pathExit);
        } catch (IOException ex) {
        }

        xPlay = (int) (getWidth() * 0.5) - (btnImgMenuWidth / 2);
        yPlay = (int) (getHeight() * 0.25) - (btnImgMenuHeight / 2);
        xHighScores = (int) (getWidth() * 0.5) - (btnImgMenuWidth / 2);
        yHighScores = (int) (getHeight() * 0.45) - (btnImgMenuHeight / 2);
        xHelp = (int) (getWidth() * 0.5) - (btnImgMenuWidth / 2);
        yHelp = (int) (getHeight() * 0.65) - (btnImgMenuHeight / 2);
        xAbout = (int) (getWidth() * 0.5) - (btnImgMenuWidth / 2);
        yAbout = (int) (getHeight() * 0.85) - (btnImgMenuHeight / 2);
        xExit = (int) getWidth() - imgExit.getWidth();
        yExit = 0;

        //System.out.println("menu " + acuanMenu + " level " + acuanLevel);

        xyArea[0] = yPlay; //x1
        xyArea[1] = yPlay + btnImgMenuWidth; //x2
        xyArea[2] = xPlay; //y1
        xyArea[3] = xPlay + btnImgMenuHeight; //y2
        btnPlay = new ButtonImage("play", imgPlayOff, imgPlayOn,
                imgPlayOff, xPlay,
                yPlay, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yHighScores; //x1
        xyArea[1] = yHighScores + btnImgMenuWidth; //x2
        xyArea[2] = xHighScores; //y1
        xyArea[3] = xHighScores + btnImgMenuHeight; //y2
        btnHighScores = new ButtonImage("highscores", imgHighScoresOff, imgHighScoresOn,
                imgHighScoresOff, xHighScores,
                yHighScores, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yHelp; //x1
        xyArea[1] = yHelp + btnImgMenuWidth; //x2
        xyArea[2] = xHelp; //y1
        xyArea[3] = xHelp + btnImgMenuHeight; //y2
        btnHelp = new ButtonImage("help", imgHelpOff, imgHelpOn,
                imgHelpOff, xHelp,
                yHelp, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yAbout; //x1
        xyArea[1] = yAbout + btnImgMenuWidth; //x2
        xyArea[2] = xAbout; //y1
        xyArea[3] = xAbout + btnImgMenuHeight; //y2
        btnAbout = new ButtonImage("about", imgAboutOff, imgAboutOn,
                imgAboutOff, xAbout,
                yAbout, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yExit; //x1
        xyArea[1] = yExit + imgExit.getHeight(); //x2
        xyArea[2] = xExit; //y1
        xyArea[3] = xExit + imgExit.getHeight(); //y2
        btnExit = new ButtonImage("exit", imgExit, imgExit,
                imgExit, xExit,
                yExit, xyArea, Graphics.TOP | Graphics.LEFT);

        for (int i = 0; i < 26; i++) {
            xBalloon[i] = (getRandomInt((int) (CANVAS_WIDTH * 0.85)));
            yBalloon[i] = (1 + getRandomInt(CANVAS_HEIGHT * 2));
        }

        for (int i = 0; i < 26; i++) {
            //System.out.println(xBalloon[i] + ", " + yBalloon[i]);
            spBalloon01[i].setRefPixelPosition(xBalloon[i], yBalloon[i]);
        }

        for (int i = 0; i < 26; i++) {
            spBalloon01[i].setFrame(0);
        }

        btnPlay.setIsPressed(true);


    }
    //start thread

    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
    }

    /*
    protected void sizeChanged(int i, int i1) {
    super.sizeChanged(i, i1);
    //System.out.println("Oit!");
    System.out.println(getWidth() + "x" +getHeight());
    }
     *
     */
    protected void drawDisplay(Graphics g) {
        g.setColor(warna);
        g.fillRect(0, 0, getWidth(), getHeight());

        for (int i = 0; i < 26; i++) {
            if (yBalloon[i] < -100) {
                //random aggy
                xBalloon[i] = (getRandomInt((int) (CANVAS_WIDTH * 0.85)));
                yBalloon[i] = (1 + getRandomInt(CANVAS_HEIGHT * 2));
                if (yBalloon[i] < (CANVAS_HEIGHT + 50)) {
                    yBalloon[i] = CANVAS_HEIGHT + 50;
                }
            } else {
                yBalloon[i] = yBalloon[i] - speedBalloon;
            }
            spBalloon01[i].setPosition(xBalloon[i], yBalloon[i]);
        }

        //bgScroll.setPosition(xBg, yBg);
        if (isStart) {
        }

        lmgr.paint(g, 0, 0);

        g.drawImage(imgTitle, CANVAS_WIDTH / 2 - (imgTitle.getWidth() / 2), 0, Graphics.TOP | Graphics.LEFT);

        //paint disini
        //sampai sini        

        btnPlay.paint(g);
        btnHighScores.paint(g);
        btnHelp.paint(g);
        btnAbout.paint(g);
        btnExit.paint(g);

        g.setColor(0xFF0000);
        //g.setFont(TypingBalloonMIDlet.LARGE_BOLD_FONT);
        g.drawString(huruf,
                CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2,
                Graphics.TOP | Graphics.LEFT);
        //flush off-screen buffer to display
        flushGraphics();
    }

    protected void keyPressed(int keyCode) {

        //System.out.println(keyCode);

        if (keyCode == -5) {
            if (btnPlay.isIsPressed()) {
                if (getWidth() > acuanDisable) {
                    midlet.showTypingCanvas640();
                } else {
                    midlet.showTypingCanvas();
                }
            } else if (btnAbout.isIsPressed()) {
                //midlet.showAboutCanvas();
            } else if (btnHelp.isIsPressed()) {
                //midlet.showHelpCanvas();
            }

        }

        if (keyCode == -7) {
            midlet.exit();
            //System.out.println("-7");
        }

        if (keyCode == -2 || keyCode == -4) {
            //pencet bawah /kanan
            if (btnPlay.isIsPressed()) {
                btnPlay.setIsNothing(true);
                btnPlay.setIsPressed(false);
                btnHighScores.setIsPressed(true);
            } else if (btnHighScores.isIsPressed()) {
                btnHighScores.setIsNothing(true);
                btnHighScores.setIsPressed(false);
                btnHelp.setIsPressed(true);
            } else if (btnHelp.isIsPressed()) {
                btnHelp.setIsNothing(true);
                btnHelp.setIsPressed(false);
                btnAbout.setIsPressed(true);
            } else if (btnAbout.isIsPressed()) {
                btnAbout.setIsNothing(true);
                btnAbout.setIsPressed(false);
                btnPlay.setIsPressed(true);
            }
        }
        if (keyCode == -1 || keyCode == -3) {
            //pencet atas / kiri
            if (btnPlay.isIsPressed()) {
                btnPlay.setIsNothing(true);
                btnPlay.setIsPressed(false);
                btnAbout.setIsPressed(true);
            } else if (btnHighScores.isIsPressed()) {
                btnHighScores.setIsNothing(true);
                btnHighScores.setIsPressed(false);
                btnPlay.setIsPressed(true);
            } else if (btnHelp.isIsPressed()) {
                btnHelp.setIsNothing(true);
                btnHelp.setIsPressed(false);
                btnHighScores.setIsPressed(true);
            } else if (btnAbout.isIsPressed()) {
                btnAbout.setIsNothing(true);
                btnAbout.setIsPressed(false);
                btnHelp.setIsPressed(true);
            }
        }
        if (keyCode == -6) {
        }

        if (keyCode == -2 || keyCode == -4) {
            //pencet bawah /kanan
            /*
            if (btnHelp.isIsPressed()) {
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);
            btnPlay.setIsPressed(true);
            } else if (btnPlay.isIsPressed()) {
            btnPlay.setIsNothing(true);
            btnPlay.setIsPressed(false);
            btnAbout.setIsPressed(true);
            } else if (btnAbout.isIsPressed()) {
            btnAbout.setIsNothing(true);
            btnAbout.setIsPressed(false);
            btnHelp.setIsPressed(true);
            }
             * 
             */
        }

        if (keyCode == -1 || keyCode == -3) {
            //pencet atas / kiri
        }

        repaint();
    }

    protected void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;

        if (x > btnExit.xImage
                && x < (btnExit.xImage + btnExit.imageWidth)
                && y > btnExit.yImage
                && y < (btnExit.yImage + btnExit.imageHeight)) {
            keyPressed(-7);
        }

        if (x > btnPlay.xImage
                && x < (btnPlay.xImage + btnPlay.imageWidth)
                && y > btnPlay.yImage
                && y < (btnPlay.yImage + btnPlay.imageHeight)) {
            btnPlay.pointerReleased(x, y);
            btnPlay.setIsPressed(true);
            
            btnHighScores.setIsNothing(true);
            btnHighScores.setIsPressed(false);
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);
            btnAbout.setIsNothing(true);
            btnAbout.setIsPressed(false);

            keyPressed(-5);
        }
        
        if (x > btnHighScores.xImage
                && x < (btnHighScores.xImage + btnImgMenuWidth)
                && y > btnHighScores.yImage
                && y < (btnHighScores.yImage + btnImgMenuHeight)) {
            btnHighScores.pointerReleased(x, y);
            
            btnHighScores.setIsPressed(true);
            btnHighScores.setIsNothing(false);
            
            btnPlay.setIsNothing(true);
            btnPlay.setIsPressed(false);
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);
            btnAbout.setIsNothing(true);
            btnAbout.setIsPressed(false);

            keyPressed(-5);
        }
        
        if (x > btnHelp.xImage
                && x < (btnHelp.xImage + btnImgMenuWidth)
                && y > btnHelp.yImage
                && y < (btnHelp.yImage + btnImgMenuHeight)) {
            btnHelp.pointerReleased(x, y);
            
            btnHelp.setIsPressed(true);
            btnHelp.setIsNothing(false);
            
            btnPlay.setIsNothing(true);
            btnPlay.setIsPressed(false);
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);
            btnAbout.setIsNothing(true);
            btnAbout.setIsPressed(false);

            keyPressed(-5);
        }
        
        if (x > btnAbout.xImage
                && x < (btnAbout.xImage + btnImgMenuWidth)
                && y > btnAbout.yImage
                && y < (btnAbout.yImage + btnImgMenuHeight)) {
            btnAbout.pointerReleased(x, y);
            
            btnAbout.setIsPressed(true);
            btnAbout.setIsNothing(false);
            
            btnPlay.setIsNothing(true);
            btnPlay.setIsPressed(false);
            btnHighScores.setIsNothing(true);
            btnHighScores.setIsPressed(false);
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);
            
            keyPressed(-5);
        }

    }

    private int getRandomInt(int upper) {
        int retVal = myRandom.nextInt() % upper;
        if (retVal < 0) {
            retVal += upper;
        }
        return (retVal);
    }

    private void drawDisablePortrait(Graphics g) {
        g.drawImage(imgDisablePortrait, 0, 0, Graphics.TOP | Graphics.LEFT);
        flushGraphics();
    }
}
